CSS3 Animation is best used for simple animations on a web page and can replace JavaScript animation. CSS animation lets an HTML element property or properties gradually change from one or more CSS property to another. One advantage of CSS animation is you don’t need a plug-in or a JavaScript library to use it which makes it a great candidate for users of HTML5 and CSS3. It is best to place all of the CSS animation classes in a separate file (e.g., animation).
Vendor prefixes are a way for browsers to support features before they are finalized because of the implementation differences between browsers. Hopefully they will not be needed in the future. Currently, most recent versions of Firefox, Opera, and IE support CSS animations without prefixes. They should be written with the non-prefixed vendor prefix placed last so that their FINAL implementation will override any current browser implementation due to the cascading rule of CSS that will interpret the last know rule that have the same set of properties. Common vendor prefixes include:
To help with creating all of the various types of vendor prefixes you can use CSS tools like AutoPrefixer or CSS3Generator or if you are a little more advanced you can use CSS preprocessor like LESS and SASS which can automatically add vendor prefixes.
Both animation property and @keyframes rules need to be prefixed. Unprefixed rules should always come last to make code future-proof.
TIP: Check caniuse.com website to see if vendor prefixes are still needed.
Numbers in the table specifies the first browser version that fully supports the property. Numbers followed by -webkit-, -moz-, -ms- , or -o- specifies the first version that worked with a prefix. See the web site http://www.caniuse.com . Browsers that do not support animation will ignore the animation rules and properties. Modernize can be used to check if CSS animations are supported and provide you the option to create a fallback approach.
Property |
IE |
Chrome |
FireFox |
Safari |
Opera |
@keyframes & animation |
10.0 |
4.0 -webkit- |
16.0, 5.0 -moz- |
4.0 -webkit- |
15.0 -webkit-, 12.1, 12.0 -o- |
Animation must be bind to a selector type by specifying at least these two properties:
The following table lists the @keyframes rule and all the animation properties:
Property | Description |
Specifies the animation keyframes |
|
A shorthand property for setting ALL the animation properties, except the animation-play-state and the animation-fill-mode property |
|
animation-name (REQUIRED) |
Specifies the name of the @keyframes animation |
animation-duration (REQUIRED) |
Specifies how many seconds or milliseconds an animation takes to complete one cycle |
Specifies when the animation will start |
|
Specifies whether or not the animation should play in reverse on alternate cycles |
|
Specifies the number of times an animation should be played |
|
Specifies the speed curve of the animation |
|
Specifies what CSS properties will apply for the element when the animation is finished or when it has a delay |
|
Specifies whether the animation is running or paused |
NOTE: Beside the required animation-name and animation-duration properties, two other animation properties that are good to assign is animation-timing-function (or easing) which determine how the speed is DISTRIBUTED across the animation (e.g., fast to slow, slow to fast, etc.)
There are two major ways of defining easing:
1. Predefined key words include:
2. The second way to define easing is to use the easing function (i.e., cubic-bezier(x1, y1, x2, y2)). The easing keywords are actually shortcuts of Bezier curves as such you can define you own. Go to http://www.cubic-bezier.com/ to define your own and then copy and paste the code into your project.
-webkit-animation-timing-function: cubic-bezier (.53, 1.46, 1,-0.49);
Ceaser is another tool that can be used that is located at http://mathewlein.com/ceaser/
The second is the animation-iteration-count which determines how many times the animation will play. The default is 1 if it is not defined.
NOTE: Two additional properties are animation-delay (how long the animation will delay before starting in units of second or milliseconds) and animation-fill-mode. Normally, the animation will move back to its original position, to prevent this set the animation-fill-mode.
The final two properties that you may want to set is the animation-direction for each iteration:
The @keyframes rule creates keyframes for the animation using a CSS property that will gradually change from the CURRENT CSS property to the NEW CSS property at those keyframes. It specifies what happen over the course of one cycle of the animation.
The example below will change the color of the box from blue to green in 6 seconds.
<!DOCTYPE html>
<html>
<head>
<style>
div { width:100px;height:100px;background:red;
-webkit-animation-name: changeColor;
-webkit-animation-duration: 6s;
animation-name:changeColor;
animation-duration: 6s;
}
@-webkit-keyframes changeColor {
from {background:blue;}
to {background:green;}
}
@keyframes changeColor {
from {background:blue;}
to {background:green;}
}
</style>
</head>
<body>
<div></div>
</body>
</html>
The example below will change the box color from blue to red to yellow to blue to green and back to blue in 6 seconds.
<!DOCTYPE html>
<html>
<head>
<style>
div { width:100px;height:100px;background:blue;
-webkit-animation-name: changeColor;
-webkit-animation-duration: 6s;
animation-name:changeColor;
animation-duration: 6s;
}
@-webkit-keyframes changeColor {
0% {background:red;}
25% {background:yellow;}
50% {background:blue;}
100% {background:green;}
}
@keyframes changeColor {
0% {background:red;}
25% {background:yellow;}
50% {background:blue;}
100% {background:green;}
}
</style>
</head>
<body>
<div></div>
</body>
</html>
The example below will change the color and animate the position of the box.
<!DOCTYPE html>
<html>
<head>
<style>
div { width:100px;height:100px;background:blue;
position:relative;
-webkit-animation-name: changeColorAndPosition;
-webkit-animation-duration: 6s;
animation-name: changeColorAndPosition;
}
@-webkit-keyframes changeColorAndPosition {
0% {background: red; left:0px; top:0px;}
25% {background: yellow; left:200px; top:0px;}
50% {background: blue; left:200px; top:200px;}
75% {background: green; left:0px; top:200px;}
100% {background: red; left:0px; top:0px;}
}
@keyframes changeColorAndPosition {
0% {background: red; left:0px; top:0px;}
25% {background: yellow; left:200px; top:0px;}
50% {background: blue; left:200px; top:200px;}
75% {background: green; left:0px; top:200px;}
100% {background: red; left:0px; top:0px;}
}
</style>
</head>
<body>
<div></div>
</body>
</html>
The example below will change the color and animate the translation properties of the box. In the previous example you use the position properties to animate the box. Here we will use CSS animation properties of translate(x,y) which will give you better performance. It is best to use Left /Top for positioning objects and translate(x,y) for moving objects. (Changes from previous example are in blue.)
<!DOCTYPE html>
<html>
<head>
<style>
div{width:100px;height:100px;background:blue;
position:relative; this property is no longer need
-webkit-animation-name: changeColorAndPosition;
-webkit-animation-duration: 6s;
animation-name: changeColorAndPosition;
animation-duration: 6s;
}
@-webkit-keyframes changeColorAndPosition {
0% {background: red; -webkit-transform:translate(0px,0px);}
25% {background: yellow; -webkit-transform:translate(200px,0px);}
50% {background: blue; -webkit-transform:translate(200px,200px);}
75% {background: green; -webkit-transform:translate(0px,200px);}
100% {background: red; -webkit-transform:translate(0px,0px);}
}
@keyframes changeColorAndPosition {
0% {background: red; -webkit-transform:translate(0px,0px);}
25% {background: yellow; -webkit-transform:translate(200px,0px);}
50% {background: blue; -webkit-transform:translate(200px,200px);}
75% {background: green; -webkit-transform:translate(0px,200px);}
100% {background: red; -webkit-transform:translate(0px,0px);} }
</style>
</head> <body>
<div></div>
</body>
</html>
The example below will use multiple animation properties. (Changes from previous example are in blue. Beside the required animation-name and animation-duration, it is best to add one animation property at a time to see the effect more clearly.)
<!DOCTYPE html>
<html>
<head>
<style>
div{width:100px;height:100px;background:blue;
position:relative; this property is no longer need
Chrome, Safari, Opera -webkit-animation-name: changeColorAndPosition;
-webkit-animation-duration: 6s; -webkit-animation-timing-function:linear;
-webkit-animation-delay:2s;
-webkit-animation-iteration-count:infinite;
-webkit-animation-direction:alternate;
-webkit-animation-play-state:running;
Standard syntax animation-name: changeColorAndPosition;
animation-duration: 6s; animation-timing-function:linear;
animation-delay:2s;
animation-iteration-count:infinite;
animation-direction:alternate;
animation-play-state:running;
}
@-webkit-keyframes changeColorAndPosition {
0% {background: red; -webkit-transform:translate(0px,0px);}
25% {background: yellow; -webkit-transform:translate(200px,0px);}
50% {background: blue; -webkit-transform:translate(200px,200px);}
75% {background: green; -webkit-transform:translate(0px,200px);}
100% {background: red; -webkit-transform:translate(0px,0px);}
}
@keyframes changeColorAndPosition {
0% {background: red; -webkit-transform:translate(0px,0px);}
25% {background: yellow; -webkit-transform:translate(200px,0px);}
50% {background: blue; -webkit-transform:translate(200px,200px);}
75% {background: green; -webkit-transform:translate(0px,200px);}
100% {background: red; -webkit-transform:translate(0px,0px);} }
</style>
</head> <body>
<div></div>
</body>
</html>
This is the same animation effect as the previous example but uses shorthand animation properties. The order does not matter except when using duration and delay, they need to be in that order.
SYNTAX: animation: name duration timing_function delay interaction_count direction;
<!DOCTYPE html>NOTE: Like CSS properties like border is used by itself, you can specify all of the value in one line.
<html>
<head>
<style>
div{width:100px;height:100px;background:blue;
position:relative; this property is no longer need
Chrome, Safari, Opera -webkit-animation: changeColorAndPosition 6s linear 2s infinite alternate running;-webkit-animation-name: changeColorAndPosition;
-webkit-animation-duration: 6s;-webkit-animation-timing-function:linear;
-webkit-animation-delay:2s;
-webkit-animation-iteration-count:infinite;
-webkit-animation-direction:alternate;
-webkit-animation-play-state:running;
Standard syntax animation: changeColorAndPosition 6s linear 2s infinite alternate running;animation-name: changeColorAndPosition;
animation-duration: 6s;animation-timing-function:linear;
animation-delay:2s;
animation-iteration-count:infinite;
animation-direction:alternate;
animation-play-state:running;
}
@-webkit-keyframes changeColorAndPosition {
0% {background: red; -webkit-transform:translate(0px,0px);}
25% {background: yellow; -webkit-transform:translate(200px,0px);}
50% {background: blue; -webkit-transform:translate(200px,200px);}
75% {background: green; -webkit-transform:translate(0px,200px);}
100% {background: red; -webkit-transform:translate(0px,0px);}
}
@keyframes changeColorAndPosition {
0% {background: red; -webkit-transform:translate(0px,0px);}
25% {background: yellow; -webkit-transform:translate(200px,0px);}
50% {background: blue; -webkit-transform:translate(200px,200px);}
75% {background: green; -webkit-transform:translate(0px,200px);}
100% {background: red; -webkit-transform:translate(0px,0px);} }
</style>
</head> <body>
<div></div>
</body>
</html>
While probably not a good idea to do for beginner, you can delete the 0% and 100% keyframes and the animation will still work correctly from its initial CSS values.
<!DOCTYPE html>NOTE: To create a HOLD keyframe (a keyframe that has the same start and end properties), create two keyframes with the different percentage values but the SAME keyframe properties separated by commas instead of having two lines. To create a hold at the start of an animation, use the animation-delay property instead.
<html>
<head>
<style>
div{width:100px;height:100px;background:blue;
position:relative; this property is no longer need
Chrome, Safari, Opera -webkit-animation: changeColorAndPosition 6s linear 2s infinite alternate running; Standard syntax animation: changeColorAndPosition 6s linear 2s infinite alternate running;
}
@-webkit-keyframes changeColorAndPosition {0% {background: red; -webkit-transform:translate(0px,0px);}
25% {background: yellow; -webkit-transform:translate(200px,0px);}
50% {background: blue; -webkit-transform:translate(200px,200px);}
75% {background: green; -webkit-transform:translate(0px,200px);}100% {background: red; -webkit-transform:translate(0px,0px);}
}
@keyframes changeColorAndPosition {0% {background: red; -webkit-transform:translate(0px,0px);}
25% {background: yellow; -webkit-transform:translate(200px,0px);}
50% {background: blue; -webkit-transform:translate(200px,200px);}
75% {background: green; -webkit-transform:translate(0px,200px);}100% {background: red; -webkit-transform:translate(0px,0px);}}
</style>
</head> <body>
<div></div>
</body>
</html>
Flying Objects Demo
Flying Objects Instructions
Typically, you don't want the animation to start when the page first load, instead, is should be started or restarted from another object (e.g., a button or some other event (e.g., a mouse click). See examples below:
Multiple Flying Objects Demo